// PropView.h : interface of the CPropView resizable propertysheet class
//
// Contents:
//
// 1. CPropTabCtrl - handler class for the propertysheet tab control
// 2. CPropView - resizable property sheet usable as a view
//
// Copyright 2002 by SoftGee
//////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_PROPVIEW_H__A312271D_3926_11D6_B652_0048548B09C5__INCLUDED_)
#define AFX_PROPVIEW_H__A312271D_3926_11D6_B652_0048548B09C5__INCLUDED_

#pragma once

#include "PropPage.h"
#include "resource.h"
/////////////////////////////////////////////////////////////////////////////
// CPropTabCtrlImpl - implements a propertysheet tab control handler

template <class T, class TBase = CTabCtrl>
class ATL_NO_VTABLE CPropTabCtrlImpl : public CWindowImpl< T, TBase>
{
public:
	BOOL SubclassWindow(HWND hWnd)
	{
		// subclass the propertysheet tab control window
		return CWindowImpl< T, TBase >::SubclassWindow(hWnd);
	}

	BEGIN_MSG_MAP(CPropTabCtrlImpl)
		MESSAGE_HANDLER(WM_WINDOWPOSCHANGED, OnWindowPosChanged)
	END_MSG_MAP()

	LRESULT OnWindowPosChanged(UINT, WPARAM, LPARAM lParam, BOOL&)
	{
		// get window position structure from lParam
		LPWINDOWPOS lpWP = (LPWINDOWPOS)lParam;

		// initialize a property sheet variable with the parent's handle
		CPropertySheet m_sheet;
		m_sheet.m_hWnd = GetParent();

		// set the size of the active property page to slightly smaller
		// than the tab control's new client area size
		::SetWindowPos(m_sheet.GetActivePage(), NULL, 0, 0, lpWP->cx - 8, 
					lpWP->cy - 25, SWP_NOMOVE);

		// release the property sheet handle since we don't own it
		m_sheet.m_hWnd = NULL;

		return 0;
	}
};

class CPropTabCtrl : public CPropTabCtrlImpl<CPropTabCtrl>
{ };


/////////////////////////////////////////////////////////////////////////////
// CPropView - resizable property sheet usable as a view

class CPropView : public CPropertySheetImpl<CPropView>,
					public CDialogResize<CPropView>
{
public:
	// property pages. Pass in dialog and control resource IDs
	CPropPage<IDD_DIALOG1, IDC_EDIT1> m_page0;
	CPropPage<IDD_DIALOG2, IDOK> m_page1;

	// tab control variable
	CPropTabCtrl m_tab;

	BOOL PreTranslateMessage(MSG* pMsg)
	{
		pMsg;
		return FALSE;
	}

	// constructor. At least one page must be added before window is created
	CPropView()
	{
		// disable Apply button
		m_psh.dwFlags |= PSH_NOAPPLYNOW;

		// title must be set before page is added
		m_page0.SetTitle(_T("Zero"));
		AddPage(m_page0);

		m_page1.SetTitle(_T("One"));
		AddPage(m_page1);

		SetActivePage(0);
	}

	// override the callback procedure and add a pre-creation message
	// handler so that we can change the window style
	static int CALLBACK PropSheetCallback(HWND hWnd, UINT uMsg, LPARAM lParam)
	{
		// dialog template is available and changeable pre-creation
		if(uMsg == PSCB_PRECREATE)
		{
			LPDLGTEMPLATE lpDT = (LPDLGTEMPLATE)lParam;

			// remove dialog border styles
			lpDT->style -= DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU;

			// add child window and clipping styles
			lpDT->style |= WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;

			return 0;
		}
		else // call inherited method
			return CPropertySheetImpl<CPropView>::PropSheetCallback(hWnd, uMsg, lParam);
	}

	// add button handlers and chain both inherited message maps
	BEGIN_MSG_MAP(CPropView)
		COMMAND_ID_HANDLER(IDOK, OnOK)
		COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
		CHAIN_MSG_MAP(CPropertySheetImpl<CPropView>)
		CHAIN_MSG_MAP(CDialogResize<CPropView>)
	END_MSG_MAP()

	// add the tab control and buttons to the resize map
	BEGIN_DLGRESIZE_MAP(CPropView)
		DLGRESIZE_CONTROL(ATL_IDC_TAB_CONTROL, DLSZ_SIZE_X | DLSZ_SIZE_Y)
		DLGRESIZE_CONTROL(IDOK, DLSZ_MOVE_X | DLSZ_MOVE_Y)
		DLGRESIZE_CONTROL(IDCANCEL, DLSZ_MOVE_X | DLSZ_MOVE_Y)
	END_DLGRESIZE_MAP()

	// Since WTL's CPropertySheet does not receive a WM_INITDIALOG message,
	// this needs to be called to handle the remaining initialization tasks
	LRESULT _Init(HWND hwndParent)
	{
		SetParent(hwndParent);

		// remove dialog modal frame, set client edge style
		ModifyStyleEx(0, WS_EX_CLIENTEDGE);

		// initialize the resize code
		DlgResize_Init(false, false, 0);

		// subclass the property sheet tab control
		m_tab.SubclassWindow(GetTabControl());

		return 0;
	}

	// button handlers
	LRESULT OnOK(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		// TODO: add code for desired action on OK click

		return 0;
	}
	LRESULT OnCancel(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		// TODO: add code for desired action on Cancel click

		return 0;
	}
};

#endif // !defined(AFX_PROPVIEW_H__A312271D_3926_11D6_B652_0048548B09C5__INCLUDED_)
